package com.seasugar.raggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
//处理类上带RestController或Controller的注解
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandle {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R exceptionHandle(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        //解决唯一键重复输入问题
        if(ex.getMessage().contains("Duplicate entry")){
            String[] split=ex.getMessage().split(" ");
            String msg=split[2]+"已存在";
            return R.error(msg);
        }
        //解决外键问题
        if(ex.getMessage().contains("FK_Relationship_1")){
            return R.error("删除失败！请先删除该菜系下的菜品");
        }
        if(ex.getMessage().contains("FK_Relationship_3")){
            return R.error("删除失败！请先删除该套餐下的套餐");
        }
        return R.error("失败了");
    }

//    public R exceptionHandle2(SQL ex){
//        log.error(ex.);
//    }
}
